package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.MessageBean;
import com.project.mapper.IMessageMapper;
import com.project.service.IMessageService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

public class MessageServiceImpl extends BaseService implements IMessageService {
    @Override
    public void add(MessageBean message) {
        SqlSession session = this.getSession();
        IMessageMapper mapper = session.getMapper(IMessageMapper.class);
        mapper.add(message);
        session.commit();
        session.close();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IMessageMapper mapper = session.getMapper(IMessageMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(int id, String tel, String post) {
        SqlSession session = this.getSession();
        IMessageMapper mapper = session.getMapper(IMessageMapper.class);
        mapper.update(id,tel,post);
        session.commit();
        session.close();
    }

    @Override
    public List<MessageBean> findAll() {
        SqlSession session = this.getSession();
        IMessageMapper mapper = session.getMapper(IMessageMapper.class);
        List<MessageBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public MessageBean findById(int id) {
        SqlSession session = this.getSession();
        IMessageMapper mapper = session.getMapper(IMessageMapper.class);
        MessageBean message = mapper.findById(id);
        session.close();
        return message;
    }

    @Override
    public List<MessageBean> findByName(String name) {
        SqlSession session = this.getSession();
        IMessageMapper mapper = session.getMapper(IMessageMapper.class);
        List<MessageBean> list = mapper.findByName("%" + name + "%");
        session.close();
        return list;
    }

    @Override
    public List<MessageBean> findByBirth(LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IMessageMapper mapper = session.getMapper(IMessageMapper.class);
        List<MessageBean> list = mapper.findByBirth(startDate,endDate);
        session.close();
        return list;
    }

    @Override
    public void addMore(List<MessageBean> list) {
        SqlSession session = this.getSession();
        IMessageMapper mapper = session.getMapper(IMessageMapper.class);
        mapper.addMore(list);
        session.commit();
        session.close();
    }

    @Override
    public List<MessageBean> findByItem(String name, String post, LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IMessageMapper mapper = session.getMapper(IMessageMapper.class);
        List<MessageBean> list = mapper.findByItem(name,post,startDate,endDate);
        session.close();
        return list;
    }

    @Override
    public CutPageBean<MessageBean> findAll(int pageNO) {
        CutPageBean<MessageBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IMessageMapper mapper = session.getMapper(IMessageMapper.class);
        cutBean.setList(mapper.cutAllList((pageNO - 1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE));
        cutBean.setCount(mapper.cutAllCount());
        session.close();
        return cutBean;
    }

    @Override
    public CutPageBean<MessageBean> cutByItem(int pageNO, String name, String post, LocalDate startDate, LocalDate endDate) {
        CutPageBean<MessageBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IMessageMapper mapper = session.getMapper(IMessageMapper.class);
        cutBean.setList(mapper.cutByList((pageNO-1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE
                ,name,post ,startDate ,endDate));
        cutBean.setCount(mapper.cutByItem(name,post,startDate,endDate));
        session.close();
        return cutBean;
    }

    public static void main(String[] args) {
        IMessageService service = new MessageServiceImpl();
//        service.add(new MessageBean("asd","132","hi",LocalDate.parse("1995-05-05")));
//        service.del(9);
//        System.out.println(service.findAll());
//        System.out.println(service.findById(5));
//        System.out.println(service.findByName("陈"));
//        System.out.println(service.findByBirth(LocalDate.parse("1995-05-01"),
//                LocalDate.parse("1998-08-08")));
//        service.addMore(Arrays.asList(new MessageBean("aaa","123","职员",LocalDate.parse("1996-05-05")),
//                new MessageBean("bbb","122","经理",LocalDate.parse("1992-04-05")),
//                new MessageBean("ccc","133","财务职员",LocalDate.parse("1993-02-05"))));
//        System.out.println(service.findByItem(null, null, null, null));
//        System.out.println(service.findAll(2));
        System.out.println(service.cutByItem(1,null,null,null,LocalDate.parse("1995-05-05")));

    }
}
